<template>
    <van-skeleton title :row="3" />
    <div>
        <van-card @click="tz(item.goods_id)" v-for="(item, index) in xqlist" :price="item.goods_price"
            :title="item.goods_name" :thumb="item.goods_small_logo" />

    </div>
    <van-pull-refresh v-model="refreshing" @refresh="onRefresh">
        <van-list v-model:loading="loading" :finished="finished" finished-text="没有更多了" @load="onLoad">
            <van-cell v-for="item in list" :key="item" :title="item" />
        </van-list>
    </van-pull-refresh>
</template>

<script setup>
import { ref } from 'vue';
import axios from 'axios';
import { useRoute } from 'vue-router';
import { useRouter } from 'vue-router';
let route = useRoute()
let router = useRouter()
const xqlist = ref([])
axios.get('https://api-hmugo-web.itheima.net/api/public/v1/goods/search', {
    params: {
        cid: route.query.cid
    }
}).then(res => {
    xqlist.value = res.data.message.goods
})
const tz = (goods_id) => {
    router.push({
        path: '/swiper',
        query: {
            goods_id
        }
    })
}
const list = ref([]);
const loading = ref(false);
const finished = ref(false);
const refreshing = ref(false);

const onLoad = () => {
    setTimeout(() => {
        if (refreshing.value) {
            list.value = [];
            refreshing.value = false;
        }

        for (let i = 0; i < 10; i++) {
            list.value.push(list.value.length + 1);
        }
        loading.value = false;

        if (list.value.length >= 40) {
            finished.value = true;
        }
    }, 1000);
};

const onRefresh = () => {
    // 清空列表数据
    finished.value = false;

    // 重新加载数据
    // 将 loading 设置为 true，表示处于加载状态
    loading.value = true;
    onLoad();
};
</script>

<style lang="scss" scoped></style>